# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
# Create your models here.


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    group = models.ForeignKey('Group')

    def __unicode__(self):
        return self.user.username

    class Meta:
        verbose_name = '用户表'
        verbose_name_plural = '用户表'


class Group(models.Model):
    name = models.CharField('组名', max_length=120)
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    changeTime = models.DateTimeField('修改时间', auto_now=True)
    project = models.ManyToManyField('Project')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '用户组'
        verbose_name_plural = '用户组'


class Project(models.Model):
    name = models.CharField('项目名', max_length=120)
    gitAddress = models.CharField('数据源', max_length=120)
    gitVersion = models.CharField('仓库版本', max_length=50, null=True, blank=True)
    defaultBranch = models.CharField('默认分支', max_length=120, null=True, blank=True)
    middleServer = models.ForeignKey('Service', related_name='middleServer')
    preServer = models.ForeignKey('Service', related_name='preServer')
    preScriptPath = models.CharField('预处理脚本', max_length=120)
    pushPath = models.CharField('推送通知', max_length=120)
    excludeList = models.TextField('排除列表')
    dstServerKey = models.ForeignKey('Key')
    serverPort = models.IntegerField('服务器端口')
    postScriptPath = models.CharField('更新后脚本地址', max_length=120)
    postServer = models.ForeignKey('Service', related_name='postServer')
    threadNum = models.IntegerField('线程数', default=1)
    sync = models.BooleanField('完全同步', default=False)
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    createUser= models.ForeignKey(User, related_name='ProjectCreateUser')
    changeUser= models.ForeignKey(User, related_name='ProjectChangeUser', null=True, blank=True)
    changeTime = models.DateTimeField('修改时间', auto_now=True)
    isTemplate = models.BooleanField('是否为项目模板', default=False)
    backupPath = models.CharField('备份路径', max_length=120, null=True, blank=True)
    status = models.ForeignKey('Status')

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = '项目表'
        verbose_name = '项目表'


class Status(models.Model):
    name = models.CharField('状态名', max_length=20)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = '公共状态表'
        verbose_name = '公共状态表'


class Task(models.Model):
    log = models.TextField('任务执行日志')
    ip = models.ForeignKey('Service')
    status = models.ForeignKey('Status')
    execUser = models.ForeignKey(User)
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    project = models.ForeignKey('Project')

    def __unicode__(self):
        return '%s-%s' % (self.project.name, self.ip)

    class Meta:
        verbose_name_plural = '任务运行记录'
        verbose_name = '任务运行记录'


class SysLog(models.Model):
    log = models.TextField('系统日志信息')
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    changeTime = models.DateTimeField('写入时间', auto_now=True)

    def __unicode__(self):
        return self.createTime

    class Meta:
        verbose_name = '系统日志'
        verbose_name_plural = '系统日志'


class Service(models.Model):
    ip = models.GenericIPAddressField('主机ip')
    hostname = models.CharField('主机名', max_length=50)
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    changeTime = models.DateTimeField('修改时间', auto_now=True)

    def __unicode__(self):
        return '%s-%s' % (self.ip, self.hostname)

    class Meta:
        verbose_name = '主机管理'
        verbose_name_plural = '主机管理'


class Key(models.Model):
    name = models.CharField('名称', max_length=120)
    username = models.CharField('用户名', max_length=120)
    keyPath = models.CharField('私钥保存路径', max_length=120)
    password = models.CharField('私钥密码', max_length=120, null=True, blank=True)
    createUser = models.ForeignKey(User, related_name='KeyCreateUser')
    ChangeUser = models.ForeignKey(User, related_name='KeyChangeUser', null=True, blank=True)
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    changeTime = models.DateTimeField('修改时间', null=True, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '服务key'
        verbose_name_plural = '服务key'


class Module(models.Model):
    name = models.CharField('模块名称', max_length=120)
    project = models.ForeignKey('Project')
    createTime = models.DateTimeField('创建时间', auto_now_add=True)
    changeTime = models.DateTimeField('修改时间', auto_now=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '模块名称'
        verbose_name_plural = '模块名称'

